//页头指令
app.directive("headerDirective",function($window,$cookieStore){
    return{
        restrict:"EA",
        templateUrl:"../directives/headerDirective.html",
        scope:{
            handle:"&"
        },
        link:function(scope,element,attr){
            scope.loginOut=function(){
                $window.location.href="index.html";
            }
           scope.show=false;
           scope.click=function(){
               console.log(111);
               console.log(element);
               scope.show=!scope.show;
           }
           scope.changeStyle=function(e){
               console.log(e.target)
               var style=angular.element(e.target).attr("data-style");
               //console.log(style);
               //把结果保存到cookie中，并设置时间期限；
               $cookieStore.style=style;
               console.log(style);
               scope.colordata=[rgb(0,155,219),rgb(98,123,145),rgb(49,84,124),rgb(0,155,219),rgb(79,90,95),rgb(247,145,61),rgb(245,162,189),rgb(215,182,146),rgb(95,74,24),rgb(174,130,203),rgb(42,188,139),rgb(231,76,60)];
               scope.show=false;
           }
        }
    }
})
//导航指令
app.directive("navDirective",function(){
    return{

        
        restrict:"EA",
        templateUrl:"../directives/navDirective.html"
    }
})
app.directive("secondaryNavDirective",function(){
    return{
        restrict:"EA",
        templateUrl:"../directives/secondaryNavDirective.html"
    }
})
//页脚指令
app.directive("footerDirective",function(){
    return{
        restrict:"EA",
        templateUrl:"../directives/footerDirective.html"
    }
})
app.directive("buttonGroupDirective",function(){
    return{
        restrict:"EA",
        templateUrl:"../directives/buttonGroupDirective.html",
        scope:{
            handle:"&"
        },
        link:function(scope,element,attr){
            element.find("button").bind("click",function(){
                var el=element[0].querySelector(".active");
                angular.element(el).removeClass("active");
                angular.element(this).addClass("active");
            })
        }
    }
})
//视频播放页 学习笔记 item指令
app.directive("noteItemDirective",function(){
    return{
        restrict:"EA",
        scope:{
            data:"="
        },
        templateUrl:"../directives/noteItemDirective.html",
        link:function(scope,element,attr){
            scope.status=true;
            scope.click=function(e){
                var parentWidth=getComputedStyle(element.children()[0]).width;
                var itemWidth=getComputedStyle(e.target).width;
                if(parentWidth==itemWidth){
                    if(scope.status){
                        angular.element(e.target).css({"overflow":"visible","white-space":"normal","line-height":"20px","word-break":"break-all"});
                    }else{
                        angular.element(e.target).css({"overflow":"hidden","white-space":"nowrap","line-height":"50px"});
                    }
                    scope.status=!scope.status;
                }
                // console.log(parentWidth);
                // console.log(getComputedStyle(e.target).width);
            }
            // console.log(element.children().find("div")[0].style.width);
        }
    }
})
//视频点播页面 问题item指令
app.directive("questionItemDirective",function(service,toast){
    return{
        restrict:"EA",
        scope:{
            data:"=",
            index:"="
        },
        templateUrl:"../directives/questionItemDirective.html",
        link:function(scope,element,attr){
            scope.status=false;
            scope.click=function(questionId){
            //    angular.element(element[0].querySelector(".answer")).removeClass("hidden");
               scope.status=!scope.status;
                if(scope.status==true){
                    service.get("http://test.huabote.com.cn:80"+"/Course/questionDetail/"+questionId,'',function(data){
                         scope.answerData=data.answers;
                    })
                } 
            }
           scope.questionReply=function(questionId){
               if(scope.questionReplyForm.content.$error.required){
                   toast("回答问题答案不能为空！")
                   return;
               }
               
               service.post(element.find("form")[0],"http://test.huabote.com.cn:80"+"/Course/answer/"+questionId,function(data){
                    scope.questionReplyContent="";
                    scope.data.answerCount=data.length;
                    scope.answerData=data;
               })
           }
        }
    }
})
app.directive("toast-directive",function(){
    return{
        restrict:"EA",
        templateUrl:"../directives/toast.html"
    }
})
